package com.cykj.admin.util;


import com.cykj.common.entity.MenuDO;

import java.util.ArrayList;
import java.util.List;

/**
 * @BelongsProject: smart_recruit_backend
 * @BelongsPackage: com.cykj.admin.util
 * @Author: 小小卢
 * @CreateTime: 2025-08-20 10:43
 * @Description: TODO
 * @Version: 1.0
 */
public class TreeMenuUtils {

    /**
     * @Description: 构建树形菜单
     * @Param: [allMenuList]：根据角色查询出来的菜单数据
     * @return: java.util.List<com.cykj.entity.CyMenuDO> 构建好的菜单
     */
    public static List<MenuDO> buildTreeMenu(List<MenuDO> allMenuList) {
        List<MenuDO> treeMenu = new ArrayList<>();

        //1.遍历菜单
        for (MenuDO item : allMenuList) {
            //2.寻找父级菜单
            if (item.getParentId() == 0){
                //3.寻找子菜单
                MenuDO menu = getChildrenMenu(item, allMenuList);
                treeMenu.add(menu);
            }
        }
        return treeMenu;
    }


    /**
     * @Description: 寻找子菜单
     * @Param: [parentMenu, allMenuList]
     * @return: com.cykj.entity.CyMenuDO
     */
    private static MenuDO getChildrenMenu(MenuDO parentMenu, List<MenuDO> allMenuList) {

        //1.遍历菜单
        for (MenuDO item : allMenuList) {
            //判断是否为parentMenu的子菜单
            if (parentMenu.getMenuId() == item.getParentId()){
                //子菜单 item
                //判断子菜单有没有子菜单
                MenuDO childrenMenu = getChildrenMenu(item, allMenuList);
                //父级菜单的子菜单
                parentMenu.getChildren().add(childrenMenu);
            }
        }
        return parentMenu;
    }
}